<?php 
namespace app\dboard\logic;
use think\Db;
use think\facade\Session;
class Acourseware extends Base
{

    function __construct()
    {}

    public function create($param)
    {
        $name = isset($param['name']) ? trim($param['name']):'';
        $course = isset($param['course']) ? trim($param['course']):'';
        $main_point = isset($param['main_point']) ? trim($param['main_point']):'';
        $teaching_aims = isset($param['teaching_aims']) ? trim($param['teaching_aims']):'';
        $can_download = isset($param['can_download']) ? intval($param['can_download']):0;
        $file= isset($param['file']) ? $param['file']:[];
        if (!$name || !$course || !$main_point || !$teaching_aims || !$file) {
            $this->res_data['Msg'] = "缺少必填信息";
            return $this->res_data;
        }
        if (!is_array($file)) {
            $this->res_data['Msg'] = "课件数据提交有误";
            return $this->res_data;
        }
        foreach ($file as $key => &$value) {
            if (count($value)!=2 || !isset($value[0]) || !isset($value[1])) {
                $this->res_data['Msg'] = "课件数据有误";
                return $this->res_data;
            }
            $courseware_type = Db::name('courseware_type')->where(['id'=>$value[0],'status'=>1])->find();
            if (!$courseware_type) {
                $this->res_data['Msg'] = "不存在该课件类型";
                return $this->res_data;
            }
            $value[1] = trim($value[1],',');
            $value[2] = $courseware_type['name'];
        }
        $courseware_course = Db::name('courseware_course')->where(['id'=>$course,'status'=>1])->find();
        if (!$courseware_type) {
            $this->res_data['Msg'] = "不存在该课件课程";
            return $this->res_data;
        }
        $data['name'] =$name;
        $data['main_point'] =$main_point;
        $data['teaching_aims'] =$teaching_aims;
        $data['can_download'] =$can_download;
        $data['course'] =$courseware_course['name'];
        $data['course_id'] =$course;
        $data['file'] = json_encode($file);
        $data['status'] =1;
        $data['create_time'] =time();
        $data['update_time'] =time();
        Db::startTrans();
        try {
            $sdata = Db::name('courseware')->insertGetId($data);
            if(!$sdata) {
                Db::rollBack();
                $this->res_data['Msg'] = '添加门店失败';
                return $this->res_data;
            }
            Db::commit();
            $this->res_data['Msg'] = '成功';
            $this->res_data['Code'] = 0;
            return $this->printResult($this->res_data);
        } catch (\Exception $e) {
            Db::rollBack();
            $this->res_data['Msg'] = '添加门店失败';
            return $this->res_data;
        }
    }
    public function edit($param)
    {
        $id = isset($param['id']) ? trim($param['id']):0;
        $can_download = isset($param['can_download']) ? intval($param['can_download']):0;
        $name = isset($param['name']) ? trim($param['name']):'';
        $course = isset($param['course']) ? trim($param['course']):'';
        $main_point = isset($param['main_point']) ? trim($param['main_point']):'';
        $teaching_aims = isset($param['teaching_aims']) ? trim($param['teaching_aims']):'';
        $file= isset($param['file']) ? $param['file']:[];
        if (!$id || !$name || !$course || !$main_point || !$teaching_aims || !$file) {
            $this->res_data['Msg'] = "缺少必填信息";
            return $this->res_data;
        }
        $courseware= Db::name('courseware')->where([['id','=',$id]])->find();
        if (!$courseware) {
            $this->res_data['Msg'] = "无此课件";
            return $this->res_data;
        }
        if (!is_array($file)) {
            $this->res_data['Msg'] = "课件数据提交有误";
            return $this->res_data;
        }
        foreach ($file as $key => &$value) {
            if (count($value)!=2 || !isset($value[0]) || !isset($value[1])) {
                $this->res_data['Msg'] = "课件数据有误";
                return $this->res_data;
            }
            $courseware_type = Db::name('courseware_type')->where(['id'=>$value[0],'status'=>1])->find();
            if (!$courseware_type) {
                $this->res_data['Msg'] = "不存在该课件类型";
                return $this->res_data;
            }
            $value[1] = trim($value[1],',');
            $value[2] = $courseware_type['name'];
        }
        $courseware_course = Db::name('courseware_course')->where(['id'=>$course,'status'=>1])->find();
        if (!$courseware_type) {
            $this->res_data['Msg'] = "不存在该课件课程";
            return $this->res_data;
        }
        $data['main_point'] =$main_point;
        $data['teaching_aims'] =$teaching_aims;
        $data['can_download'] =$can_download;
        $data['file'] = json_encode($file);
        $data['name'] =$name;
        $data['course'] =$courseware_course['name'];
        $data['course_id'] =$course;
        $data['update_time'] =time();
        $sdata = Db::name('courseware')->where([['id','=',$id]])->data($data)->update();
        if(!$sdata) {
            $this->res_data['Msg'] = '编辑失败';
            return $this->res_data;
        }
        $this->res_data['Msg'] = '成功';
        $this->res_data['Code'] = 0;
        return $this->printResult($this->res_data);
    }
    public function updateCourseware($param)
    {
        $id = isset($param['id']) ? intval($param['id']):0;
        $status = isset($param['status']) ? intval($param['status']):0;
        $courseware= Db::name('courseware')->where([['id','=',$id]])->find();
        if (!$courseware) {
            $this->res_data['Msg'] = "无此课件";
            return $this->res_data;
        }
        $data['status'] =$status;
        $data['update_time'] =time();
        $sdata = Db::name('courseware')->where([['id','=',$id]])->data($data)->update();
        if(!$sdata) {
            $this->res_data['Msg'] = '操作失败';
            return $this->res_data;
        }
        $this->res_data['Msg'] = '成功';
        $this->res_data['Code'] = 0;
        return $this->printResult($this->res_data);
    }
    //门店页信息
    public function coursewareInfo($param)
    {   
        $id = isset($param['id'])?intval($param['id']):0;
        if (!$id){
            $this->res_data['Msg'] = "无此课件";
            return $this->res_data;
        }
        $info= Db::name('courseware')->where([['id','=',$id]])->find();
        if (!$info){
            $this->res_data['Msg'] = "无此课件";
            return $this->res_data;
        }
        if (isset($info['file']) && $info['file']) {
            $info['file'] = json_decode($info['file'],1);
        }
        $pid3 = $info['course_id'];
        $pid2 = Db::name('courseware_course')->where(['id'=>$pid3])->value('pid');
        $pid1 = Db::name('courseware_course')->where(['id'=>$pid2])->value('pid');
        // $course1 = Db::name('courseware_course')->where(['status'=>1,'level'=>1])->select();
        $course2 = Db::name('courseware_course')->where(['status'=>1,'pid'=>$pid1])->select();
        $course3 = Db::name('courseware_course')->where(['status'=>1,'pid'=>$pid2])->select();
        $courseware_type = Db::name('courseware_type')->where(['status'=>1])->select();

        $this->res_data['Msg'] = '成功';
        $this->res_data['Code'] = 0;
        $this->res_data["Data"] = ['courseware'=>$info,'pid1'=>$pid1,'pid2'=>$pid2,'pid3'=>$pid3,'course2'=>$course2,'course3'=>$course3,'courseware_type'=>$courseware_type];
        return $this->printResult($this->res_data);
    }

    public function updateCoursewareauthorize($param)
    {
        $course_ids = isset($param['course_ids']) ? $param['course_ids']:0;
        $sids = isset($param['sids']) ? intval($param['sids']):0;
        $start_time = isset($param['start_time']) ? trim($param['start_time']):'';
        $end_time = isset($param['end_time']) ? trim($param['end_time']):'';
        if (!$sids || !$course_ids || !$start_time || !$end_time) {
            $this->res_data['Msg'] = "缺少参数";
            return $this->res_data;
        }
        if (!is_array($course_ids)) {
            $this->res_data['Msg'] = "参数格式不正确";
            return $this->res_data;
        }
        $store = Db::name('store')->where(['id'=>$sids,'status'=>1])->field('id')->find();
        if (!$store) {
            Db::rollBack();
            $this->res_data['Msg'] = "门店不存在";
            return $this->res_data;
        }
        $start_time = strtotime($start_time);
        $end_time = strtotime($end_time);
        Db::startTrans();
        try {
            // $course_ids_arr = explode(',', trim($course_ids,','));
            if (Db::name('courseware_authorize')->where(['sid'=>$sids,'status'=>1,'type'=>1])->find()) {
                $is_update = Db::name('courseware_authorize')->where(['sid'=>$sids,'status'=>1,'type'=>1])->update(['status'=>0,'update_time'=>time()]);
                if (!$is_update) {
                    Db::rollBack();
                    $this->res_data['Msg'] = "授权出错";
                    return $this->res_data;
                }
            }
            $data = [];
            foreach ($course_ids as $key1 => $value1) {
                $data[$key1]['course_id'] = $value1;
                $data[$key1]['sid'] = $sids;
                $data[$key1]['status'] = 1;
                $data[$key1]['create_time'] = time();
                $data[$key1]['start_time'] = $start_time;
                $data[$key1]['end_time'] = $end_time;
            }
            $is_insert = Db::name('courseware_authorize')->insertAll($data);
            if (!$is_insert) {
                Db::rollBack();
                $this->res_data['Msg'] = "授权出错";
                return $this->res_data;
            }
            Db::commit();
            $this->res_data['Msg'] = '成功';
            $this->res_data['Code'] = 0;
            return $this->printResult($this->res_data);
        } catch (\Exception $e) {
            Db::rollBack();
            $this->res_data['Msg'] = "授权出错";
            return $this->res_data;
        }
        $this->res_data['Msg'] = '成功';
        $this->res_data['Code'] = 0;
        return $this->printResult($this->res_data);
    }

    public function add_type($param)
    {
        $name = isset($param['name']) ? trim($param['name']):'';
        if (!$name) {
            $this->res_data['Msg'] = "请输入名称";
            return $this->res_data;
        }
        $data['name'] =$name;
        $data['status'] =1;
        $data['create_time'] =time();
        $data['update_time'] =time();
        Db::startTrans();
        try {
            $sdata = Db::name('courseware_type')->insertGetId($data);
            if(!$sdata) {
                Db::rollBack();
                $this->res_data['Msg'] = '添加课件类型失败';
                return $this->res_data;
            }
            Db::commit();
            $this->res_data['Msg'] = '成功';
            $this->res_data['Code'] = 0;
            return $this->printResult($this->res_data);
        } catch (\Exception $e) {
            Db::rollBack();
            $this->res_data['Msg'] = '添加课件类型失败';
            return $this->res_data;
        }
    }
    public function edit_type($param)
    {
        $id = isset($param['id']) ? trim($param['id']):0;
        $name = isset($param['name']) ? trim($param['name']):'';
        if (!$id || !$name) {
            $this->res_data['Msg'] = "缺少必填信息";
            return $this->res_data;
        }
        $courseware_type= Db::name('courseware_type')->where([['id','=',$id]])->find();
        if (!$courseware_type) {
            $this->res_data['Msg'] = "无此课件类型";
            return $this->res_data;
        }
        $data['name'] =$name;
        $data['update_time'] =time();
        $sdata = Db::name('courseware_type')->where([['id','=',$id]])->data($data)->update();
        if(!$sdata) {
            $this->res_data['Msg'] = '编辑失败';
            return $this->res_data;
        }
        $this->res_data['Msg'] = '成功';
        $this->res_data['Code'] = 0;
        return $this->printResult($this->res_data);
    }
    public function updateCoursewareType($param)
    {
        $id = isset($param['id']) ? intval($param['id']):0;
        $status = isset($param['status']) ? intval($param['status']):0;
        $courseware= Db::name('courseware_type')->where([['id','=',$id]])->find();
        if (!$courseware) {
            $this->res_data['Msg'] = "无此课件类型";
            return $this->res_data;
        }
        $data['status'] =$status;
        $data['update_time'] =time();
        $sdata = Db::name('courseware_type')->where([['id','=',$id]])->data($data)->update();
        if(!$sdata) {
            $this->res_data['Msg'] = '操作失败';
            return $this->res_data;
        }
        $this->res_data['Msg'] = '成功';
        $this->res_data['Code'] = 0;
        return $this->printResult($this->res_data);
    }

    public function add_course($param)
    {
        $name = isset($param['name']) ? trim($param['name']):'';
        $sort = isset($param['sort']) ? intval($param['sort']):0;
        $icon = isset($param['icon']) ? trim($param['icon']):'';
        $pid1 = isset($param['pid1']) ? intval($param['pid1']):'';
        $pid2 = isset($param['pid2']) ? intval($param['pid2']):'';
        if (!$name) {
            $this->res_data['Msg'] = "请输入名称";
            return $this->res_data;
        }
        $data['name'] =$name;
        $data['sort'] =$sort;
        $data['pid'] = $pid2;
        $data['level'] = 3;
        if (!$pid2) {             // 第二级
            $data['pid'] = $pid1;
            $data['level'] = 2;
        }
        if (!$pid1) {             // 第一级
            $data['pid'] = 0;
            $data['level'] = 1;
        }
        if ($icon) {
            $data['icon'] = $icon;
        }
        $data['status'] =1;
        $data['create_time'] =time();
        $data['update_time'] =time();
        Db::startTrans();
        try {
            $sdata = Db::name('courseware_course')->insertGetId($data);
            if(!$sdata) {
                Db::rollBack();
                $this->res_data['Msg'] = '添加所属课程失败';
                return $this->res_data;
            }
            Db::commit();
            $this->res_data['Msg'] = '成功';
            $this->res_data['Code'] = 0;
            return $this->printResult($this->res_data);
        } catch (\Exception $e) {
            Db::rollBack();
            $this->res_data['Msg'] = '添加所属课程失败';
            return $this->res_data;
        }
    }
    public function edit_course($param)
    {
        $id = isset($param['id']) ? trim($param['id']):0;
        $sort = isset($param['sort']) ? trim($param['sort']):0;
        $name = isset($param['name']) ? trim($param['name']):'';
        $pid1 = isset($param['pid1']) ? intval($param['pid1']):'';
        $pid2 = isset($param['pid2']) ? intval($param['pid2']):'';
        $icon = isset($param['icon']) ? trim($param['icon']):'';
        if (!$id || !$name) {
            $this->res_data['Msg'] = "缺少必填信息";
            return $this->res_data;
        }
        $courseware_course= Db::name('courseware_course')->where([['id','=',$id]])->find();
        if (!$courseware_course) {
            $this->res_data['Msg'] = "无此课件课程";
            return $this->res_data;
        }
        $data['name'] =$name;
        $data['sort'] =$sort;
        $data['pid'] = $pid2;
        $data['level'] = 3;
        if (!$pid2) {             // 第二级
            $data['pid'] = $pid1;
            $data['level'] = 2;
        }
        if (!$pid1) {             // 第一级
            $data['pid'] = 0;
            $data['level'] = 1;
        }
        if ($icon) {
            $data['icon'] = $icon;
        }
        $data['update_time'] =time();
        $sdata = Db::name('courseware_course')->where([['id','=',$id]])->data($data)->update();
        if(!$sdata) {
            $this->res_data['Msg'] = '编辑失败';
            return $this->res_data;
        }
        // 课件里的类型数据更新
        $coursewares = Db::name('courseware')->where(['course_id'=>$id,'status'=>1])->find();
        if ($coursewares) {
            $is_update = Db::name('courseware')->where(['course_id'=>$id,'status'=>1])->update(['course'=>$name,'update_time'=>time()]);
        }
        $this->res_data['Msg'] = '成功';
        $this->res_data['Code'] = 0;
        return $this->printResult($this->res_data);
    }
    public function updateCoursewareCourse($param)
    {
        $id = isset($param['id']) ? intval($param['id']):0;
        $status = isset($param['status']) ? intval($param['status']):0;
        $courseware= Db::name('courseware_course')->where([['id','=',$id]])->find();
        if (!$courseware) {
            $this->res_data['Msg'] = "无此课件课程";
            return $this->res_data;
        }
        $data['status'] =$status;
        $data['update_time'] =time();
        $sdata = Db::name('courseware_course')->where([['id','=',$id]])->data($data)->update();
        if(!$sdata) {
            $this->res_data['Msg'] = '操作失败';
            return $this->res_data;
        }
        $this->res_data['Msg'] = '成功';
        $this->res_data['Code'] = 0;
        return $this->printResult($this->res_data);
    }

}

?>
